Basic usage of matplotlib

Matplotlib is the module of choice whenever you want to make a niceplot.


In [ ]:
# the following two lines are required to run the notebook on mybinder. 
import matplotlib 
matplotlib.use('Agg')

import numpy as np
import matplotlib.pyplot as plt # this is the only line required to make plots with matplotlibt

# this line allows you to see the plot inside the notebook
%matplotlib inline

In [ ]:
# This is a simple example to make an scatter plot
plt.figure() # Create and empty figure
x = [3,4,5]
y = [-1,2,2]
plt.scatter(x,y)

In [ ]:
# This is another plot where we save the figure at the end as png file.

x = np.linspace(0,5,10)
y = x**2 

plt.figure() # creates an empty figure

plt.scatter(x,y) # scatter plot

plt.xlabel("x") 
plt.ylabel("y") 
plt.title("Simple scatter plot") 

plt.savefig("thefig.png") # Saves the figure

In [ ]:
plt.figure()
x = np.linspace(-5.0,5.0,100)
y = 1./(1.+x**2)

plt.plot(x,y) # This time a line connect the points
plt.title(u"$\lambda$") # you can use latex

In [ ]:
# After initializing plt.figure() you can make multiple plots 
plt.figure()
plt.plot(range(10),2*np.arange(10))
plt.plot(range(10),3*np.arange(10))
plt.scatter(range(10),3*np.arange(10))

In [ ]:
# Now let's make some simple figures
# In this case: circles 

theta=np.linspace(0, 2*np.pi,50) # 50 points between 0 and 2pi

plt.figure()

plt.axis("equal")# this gives the same apparent size to the x and y axis
plt.axis("off") # this removes the axis

for i in range(5):
    # Elegir el centro del círculo
    x = 2*np.random.random()-1 # random value for the center in x
    y = 2*np.random.random()-1 # random value for the center in y
    r = np.random.random() # random value for the radius
    plt.plot(r * np.cos(theta) + x, r * np.sin(theta) + y)

Subplots

You can produce multiple plots inside the same figure.


In [ ]:
plt.figure(figsize=(10,5))
a = np.linspace(0,2*np.pi,100)

plt.subplot(2,2,1)
plt.plot(a,np.sin(a))
plt.title("sin")

plt.subplot(2,2,2)
plt.plot(a,np.cos(a))
plt.title(u"cos")

plt.subplot(2,2,3)
plt.plot(a,np.tan(a))
plt.ylim(-2,2)
plt.title(u"tan")

plt.subplot(2,2,4)
plt.plot(a,np.cos(a)**2)
plt.title(r"$\cos^2$")
plt.subplots_adjust(hspace=.5) # adjusts the horizontal space between the subplots

In [ ]:
# this an almost empty figure, only plotting text, so that you can see what is the ordering when you call plt.subplot(5,5,k)

plt.figure(figsize=(10,10))
k = 1
for n in range(1,6):
    for d in range(1,6):
        plt.subplot(5,5,k)
        plt.plot()
        plt.text(0,0,"k="+str(k))
        plt.axis("off") # esto elimina los ejes
        plt.axis("equal")
        k = k + 1

In [ ]:
# Drawing some roses
# http://en.wikipedia.org/wiki/Rose_%28mathematics%29
plt.figure(figsize=(10,10))
k = 1
for n in range(1,6):
    for d in range(1,6):
        plt.subplot(5,5,k)
        kah = n/d
        a = np.linspace(0,2*d*np.pi,200)
        r = np.sin(kah * a)
        x = r * np.cos(a)
        y = r * np.sin(a)
        plt.plot(x,y)
        plt.axis("off") 
        plt.axis("equal")
        k = k + 1
plt.show()

Legends


In [ ]:
# Adding legends to different lines in the same plot
plt.figure()
x = np.linspace(0,2*np.pi,20)
plt.plot(x,np.sin(x),"or-",label='sin(x)')
plt.plot(x,np.cos(x),"ob-",label='cos(x)')
plt.legend()
plt.axis([0,2*np.pi,-1,1])

Exercise 4.1

Make a plot of the Lissajous curve corresponding to $a=5$, $b=4$. See https://en.wikipedia.org/wiki/Lissajous_curve